﻿/*!	@file	r_gui_server.cpp
	@brief	GUI制御用
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_gui_server.h"
#include "r_gui_draw.h"
#include "r_gui_allocator.h"
#include <algorithm>

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace gui{
RRTTI_REFLECTION_IMPLEMENT ( r::gui::Server, r::rtti::ClassBase,
							  RRTTI_REFLECTION_IMPLEMENT_TYPE ( widgets ),
							)
// <<<<<<<<<< RTTI <<<<<<<<<<

RSINGLETONGET_IMPLEMENT ( Server, Server )

RALLOC_PLACEMENT_NEW_DELETE_IMPLEMENT( Server, r::gui::AllocatorData()() )

		Server :: Server()
			:
			guiDraw ( new draw::Draw() )
		{
		}

		Server :: ~Server()
		{
			for ( Widget_array::iterator it = parentWidgets.begin(); it != parentWidgets.end(); ++it ) {
				delete ( *it );
			}
			for ( Widget_array::iterator it = widgets.begin(); it != widgets.end(); ++it ) {
				delete ( *it );
			}
		}

		void	Server :: update()
		{
			// ペアレントリストのGUIを追加する
			for ( Widget_array::iterator it = parentWidgets.begin(); it != parentWidgets.end(); ++it ) {
				widgets.push_back ( *it );
			}
			parentWidgets.clear();
			// 削除リストのGUIを削除する
			for ( Widget_array::iterator it = removeWidgets.begin(); it != removeWidgets.end(); ++it ) {
				if ( ( *it )->parent == NULL ) {
					Widget_array::iterator tmp =
						std::find ( widgets.begin(), widgets.end(), ( *it ) );
					if ( tmp != widgets.end() ) {
						widgets.erase ( tmp );
					}
				}
				( *it )->remove();
				( *it )->destroy ( eventData );
				delete ( *it );
			}
			removeWidgets.clear();

			// イベントの外部入力を更新
			eventData.update();

			for ( Widget_array::reverse_iterator it = widgets.rbegin(); it != widgets.rend(); ++it ) {
				( *it )->begin ( eventData );
			}

			for ( Widget_array::reverse_iterator it = widgets.rbegin(); it != widgets.rend(); ++it ) {
				( *it )->updateWorldPosition ( 0, 0 );
			}
			if ( eventData.getLMBDClick() == true ) {
				for ( Widget_array::reverse_iterator it = widgets.rbegin(); it != widgets.rend(); ++it ) {
					( *it )->updateDoubleCkickInRectLMB ( eventData );
				}
			} else if ( eventData.getLMBClick() == true ) {
				for ( Widget_array::reverse_iterator it = widgets.rbegin(); it != widgets.rend(); ++it ) {
					( *it )->updateCkickInRectLMB ( eventData );
				}
			} else if ( eventData.getLMBPress() == true ) {
				for ( Widget_array::reverse_iterator it = widgets.rbegin(); it != widgets.rend(); ++it ) {
					( *it )->updateDragLMB ( eventData );
					( *it )->updateDragHoverInRectLMB ( eventData );
				}
			} else if ( eventData.getLMBRelease() == true ) {
				for ( Widget_array::reverse_iterator it = widgets.rbegin(); it != widgets.rend(); ++it ) {
					( *it )->updateDragReleaseInRectLMB ( eventData );
					( *it )->updateReleaseInRectLMB ( eventData );
					( *it )->updateReleaseOutRectLMB ( eventData );
				}
			}
			// ホバーチェック
			else {
				for ( Widget_array::reverse_iterator it = widgets.rbegin(); it != widgets.rend(); ++it ) {
					( *it )->updateHoverInRect ( eventData );
				}
			}

			// 更新
			for ( Widget_array::reverse_iterator it = widgets.rbegin(); it != widgets.rend(); ++it ) {
				( *it )->update ( eventData );
			}
		}

		void	Server :: draw()
		{
			guiDraw->begin();
			// 描画
			for ( Widget_array::iterator it = widgets.begin(); it != widgets.end(); ++it ) {
				( *it )->draw ( *guiDraw );
			}
			// 描画バッファをスワップする
			guiDraw->update();
		}

		void	Server :: remove ( Widget*	widget )
		{
			// リストから削除は実際の削除時に実行単に削除リストへの登録を実行
			removeWidgets.push_back ( widget );
		}

		void	Server :: unParent ( Widget*	child, Widget*	newParent )
		{
			if ( ( child->parent == NULL ) && ( newParent != NULL ) ) {
				// リストから削除
				Widget_array::iterator it =
					std::find ( widgets.begin(), widgets.end(), child );
				if ( it != widgets.end() ) {
					remove ( child );
				}
			} else if ( child->parent != NULL ) {
				child->remove();
			}
			if ( newParent != NULL ) {
				// 新しい親にペアレント
				newParent->addChild ( child );
			}
		}

		void	Server :: addChild ( Widget*	child )
		{
			parentWidgets.push_back ( child );
		}

		draw::Draw*	Server :: getGUIDraw() const
		{
			return guiDraw;
		}


		const	DrawInfo&	Server :: getDrawInfo() const
		{
			return guiDrawInfo;
		}
	}
}